layui.use(['table', 'jquery', 'element', 'form', 'layer'], function () {
    var table = layui.table;
    var $ = layui.$;
    var element = layui.element;
    var form = layui.form;


    table.render({
        elem: '#table'
        , url: '/emp/getDepartment'
        , where: {
            search: 0
        }
        , id: "department-table"
        , page: true
        , height: 512
        , limit: 10
        , cols: [[
            {field: 'dName', title: '部门'}
            , {field: 'mission', title: '职责'}
            // ,{field:'birthday',title:'出生年月'}
            // ,{field:'idCard',title:'身份证'}
            // ,{field:'area',title:'区域'}
            // ,{field:'nativePlace',title:'籍贯'}
            // ,{field:'state',title:'状态'}
            , {field: 'mName', title: '经理'}
            , {field: 'remarks', title: '备注'}
            , {field: 'operation', title: '操作', fixed: 'right', width: 110, align: 'center', toolbar: '#barDemo'}
        ]]
    });

    var dNameInput = $('#dName');
    var managerInput = $("#manager");
    var explain = $("#explain");
    var newDepartment = $('#newDepartment');
    var departmentForm = $('#newDepartmentForm');
    var newDepartmentSubmit = $('#newSubmit');
    var editDepartmentSubmit = $('#editSubmit');


    editDepartmentSubmit.hide();
    departmentForm.hide();


    table.on('tool(department-table)', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        console.log(data);
        if (layEvent === 'edit') {

            form.val("newDepartmentForm", {
                "id": data.id,
                "dName": data.dName,
                "mission": data.mission,
                "remarks": data.remarks,
                "manager": data.manager
                // "state":data.state,
                // "department":data.department,
                // "level":data.level
            });


            dNameInput.attr("disabled", true);
            managerInput.attr("disabled", true);
            explain.text("部门经理变更请转人事调动");
            editDepartmentSubmit.show();
            newDepartmentSubmit.hide();

            layer.open({
                title: "编辑部门",
                type: 1,
                area: 'auto',
                maxWidth: 1000,
                content: $("#newDepartmentForm"),
                cancel: function (index, layero) {
                    dNameInput.attr("disabled", false);
                    managerInput.attr("disabled", false);
                    explain.text("请输入在职员工员工编号,若暂无经理，请填0");
                    newDepartmentSubmit.show();
                    editDepartmentSubmit.hide();
                    departmentForm[0].reset();
                    departmentForm.hide();
                }
            });
            departmentForm.show();
        } else if (layEvent === 'access') {

            layer.open({
                title: "警告",
                content: '您没有此权限'
            });

        } else if (layEvent === 'del') {
            layer.confirm('确认删除吗？', {icon: 3, title: '确认'}, function (index) {
                deleteDepartment(data.id);
                layer.close(index);
            })
        }
    });


    function deleteDepartment(id) {
        $.post('/emp/deleteDepartment', {id: id}, function (data) {
            if (!data.status) {
                layer.open({title: '警告', content: data.msg});
            } else {
                layer.open({title: '成功', content: '删除成功'});
                table.reload('department-table');
            }
        })
    }

    element.on('tab(tab)', function (data) {
        var index = data.index;
        console.log(index);
        if (index == 0) {
            location.href = '/emp';
        } else if (index == 1) {
            location.href = '/emp/department';
        } else if (index == 2) {
            location.href = '/emp/entry';
        }
        return false;
    });


    newDepartment.click(function () {

        layer.open({
            title: "新增部门",
            type: 1,
            area: 'auto',
            maxWidth: 1000,
            content: $("#newDepartmentForm")
        });
        departmentForm.show();
        return false;
    });


    form.on('submit(newDepartmentSubmit)', function (data) {
        $.post('/emp/newDepartment', data.field, function (data) {
            if (!data.status) {
                layer.open({title: '警告', content: data.msg});
            } else {
                departmentForm[0].reset();
                layer.close(layer.index);
                layer.open({
                    title: '成功',
                    content: "新增部门成功！"
                });
                table.reload('department-table');
            }
        });
        return false;
    });

    form.on('submit(editDepartmentSubmit)', function (data) {
        $.post('/emp/editDepartment', data.field, function (data) {
            if (!data.status) {
                layer.open({title: '警告', content: data.msg});
            } else {

                dNameInput.attr("disabled", false);
                managerInput.attr("disabled", false);
                explain.text("请输入在职员工员工编号,若暂无经理，请填0");
                newDepartmentSubmit.show();
                editDepartmentSubmit.hide();
                departmentForm[0].reset();
                departmentForm.hide();

                layer.closeAll();
                layer.open({
                    title: '成功',
                    content: "编辑部门成功！"
                });
                table.reload('department-table');
            }
        });
        return false;
    });

    form.on('submit(search)', function (data) {
        table.reload('department-table', {
            where: {
                search: data.field.id
            }
        });

        return false;
    });


});